package com.xw.unification.condition;

import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Optional;

import static com.xw.unification.constant.UnificationConstant.UNIFIED_EXCEPTION_ENABLE_PROPERTY_KEY;
import static com.xw.unification.constant.UnificationConstant.DEFAULT_UNIFIED_EXCEPTION_ENABLE;

public class UnifiedExceptionCondition extends UnificationCondition {

    @Override
    public boolean matchesCondition(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return "true".equalsIgnoreCase(
                Optional.ofNullable(context.getEnvironment().getProperty(UNIFIED_EXCEPTION_ENABLE_PROPERTY_KEY))
                        .orElse(String.valueOf(DEFAULT_UNIFIED_EXCEPTION_ENABLE))
        );
    }
}
